/**
 * 终止请求
 */
import { defineStore } from "pinia";

const terminateRequest = defineStore(
    'terminateRequest',
    {
        state: () => ({
            abortReq:{}
        }),
        actions: {
            $setAbortReq(url,requestFun){
                this.abortReq[url] = requestFun
            },
            $terminateReques() {
                const abortReqKeys = Object.keys(this.abortReq)
                if (!abortReqKeys.length) {
                    return
                }
                console.log(abortReqKeys,this.abortReq)
                return new Promise((resolve, reject) => {
                    try {
                        abortReqKeys.forEach(item => {
                            this.abortReq[item].abort()
                            delete  this.abortReq[item]
                        })
                        resolve(true)
                    } catch (err) {
                        throw err
                    }
                });
            }
        }
    })

export default terminateRequest
